package com.gigmissions.server.servlets;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gigmissions.server.dao.Gig;
import com.gigmissions.server.dao.PMF;


public class GetGigServlet extends HttpServlet {
	/**
	 * 
	 */
	
	String header = "<kml xmlns=\"http://www.opengis.net/kml/2.2\"><Document><name>Gig Missions Gigs</name>";
	String titleOpen = "<Placemark><name>";
	String titleClose = "</name>";
	String descOpen = "<description><![CDATA[";
	String descClose = "]]></description>";
	String pointOpen = "<Point>";
	String pointClose = "</Point>";
	String coordOpen = "<coordinates>";
	String coordClose = "</coordinates>";
	String placeClose = "</Placemark>";
	String footer = "</Document></kml>";
	String openBreak = "<br>";
	
	String extededDataOpen = "<ExtendedData>";
	String extededDataClose = "</ExtendedData>";
	
	
	
	private static final long serialVersionUID = -2229998267832868443L;
	private static final Logger log = Logger.getLogger(GetGigServlet.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
       
    	    	
        PersistenceManager pm = PMF.get().getPersistenceManager();
        String query = "select from " + Gig.class.getName();
        List<Gig> gigs = (List<Gig>) pm.newQuery(query).execute();
        
       StringBuilder sb = new StringBuilder(header);
       
        
        if (gigs.isEmpty()) {
        	
        	//ficitcious gig
        	sb.append(titleOpen);
        	sb.append("test title");
        	sb.append(titleClose);
        	sb.append(descOpen);
        	sb.append("Posted by:");
           	sb.append("anonymous");
            sb.append("07/12/2009");
            sb.append("Fake address");
            sb.append("face description");
            sb.append(descClose);
            sb.append(pointOpen);
            sb.append(coordOpen);
            sb.append("-122.0250403");
            sb.append(",");
            sb.append("37.4163228");
            sb.append(coordClose);
            sb.append(pointClose);
            sb.append(placeClose);
            
        	
        	        	        	
        } else {
            for (Gig g : gigs) {
            	
            	if(g.getLat()!=null||g.getLng()!=null){
	            	sb.append(titleOpen);
	            	sb.append(g.getTitle());
	            	sb.append(titleClose);
	            	sb.append(descOpen);
	            	sb.append("Posted by:");
	                if (g.getAuthor() == null) {
	                	sb.append("anonymous");
	                } else {
	                	sb.append(g.getAuthor().getNickname());
	                }
	                sb.append(openBreak);
	                sb.append(g.getDate().toString());
	                sb.append(openBreak);
	                sb.append(g.getAddress());
	                sb.append(openBreak);
	                sb.append(g.getDescription());
	                sb.append(openBreak);
	                sb.append(descClose);
	                sb.append(pointOpen);
	                sb.append(coordOpen);
	                sb.append(g.getLng());
	                sb.append(",");
	                sb.append(g.getLat());
	                sb.append(coordClose);
	                sb.append(pointClose);
	                sb.append(placeClose);
            	}

            }
        }
        sb.append(footer);
        
        pm.close();
    	
    	
    	resp.setContentType("text/plain");
    	resp.getWriter().write(sb.toString());
    	resp.getWriter().flush();
    	resp.getWriter().close();

    }
	

}
